package com.study.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.study.bean.Student;
import com.study.service.StudentService;
import com.study.service.impl.StudentServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author mengxj
 */
//@ResponseBody
//@Controller
@RestController
@RequestMapping("student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String test(){
        return "hello";
    }

    /**
     * 这样不好用，这是开发者自己创建的对象，
     *  StudentService studentService = new StudentServiceImpl();
     *  而我们需要的是框架创建的对象，如下
     *  private StudentService studentService;
     *
     *  这种将创建对象的权限给了其他角色（不是开发者），我们称之为 控制反转(IOC)
     *
     * @return
     */
    @GetMapping("names1")
    public List<String> studentNames(){
        return this.studentService.studentNames();
    }

    @GetMapping("students1")
    public List<Student> students1(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return this.studentService.students1(pageNum, pageSize);
    }

    @GetMapping("students2")
    public List<Student> students2(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return this.studentService.students2(pageNum, pageSize);
    }

    @GetMapping("students21")
    public PageInfo<Student> students2_1(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return this.studentService.students2_1(pageNum, pageSize);
    }

    @GetMapping("students3")
    public List<Student> students3(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return this.studentService.students3(pageNum, pageSize);
    }

    @GetMapping("students31")
    public IPage<Student> students31(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return this.studentService.students31(pageNum, pageSize);
    }

    /**
     * 根据条件查询Student
     *   假如以下某一个条件不存在，则不要这个条件
     * @param age 年龄，大于条件 非必传
     * @param address 地址，模糊查询条件 非必传
     * @param orderByColumn 列 排序列，先假设不存在输入不对的情况 必传
     * @return 学生列表
     */
    @GetMapping("student-by-param")
    public List<Student> studentByParam(@RequestParam(required = false) Integer age,
                                        @RequestParam(required = false) String address,
                                        @RequestParam String orderByColumn){
        return this.studentService.studentByParam(age, address, orderByColumn);
    }

    @GetMapping("student-by-param-plus")
    public List<Student> studentByParamPlus(@RequestParam(required = false) Integer age,
                                        @RequestParam(required = false) String address,
                                        @RequestParam String orderByColumn){
        return this.studentService.studentByParamPlus(age, address, orderByColumn);
    }

    //直接pageNum  1 size 3
    @GetMapping("student-by-param-plus-page")
    public IPage<Student> studentByParamPlusPage(@RequestParam(required = false) Integer age,
                                            @RequestParam(required = false) String address,
                                            @RequestParam String orderByColumn){
        return this.studentService.studentByParamPlusPage(age, address, orderByColumn);
    }

    @GetMapping("insert")
    public String insert(){
        return this.studentService.insert();
    }


}
